**this code is meant to be parallel to 6.databuild_listprice.do  As a result, a lot of this code is not actually necessary to compute the contract dated index.
*see also notes to 4.databuild_closingdated.do
local msamacro="pho dc vegas sanfran sandiego la chi denver seattle"
set more off
foreach j in `msamacro' {
use temp11e_`j'_r2,clear
gen trash="`j'"
gen thresh=1
replace thresh=13 if trash=="pho"
replace thresh=25 if trash=="seattle"


sort lowaddress timeid
gen chnglist=ln(price)-ln(price[_n-1]) if lowaddress==lowaddress[_n-1] 
gen timediff=timeid-timeid[_n-1] if lowaddress==lowaddress[_n-1] 
gen my0=(year(lastsaledate)-1988)*12+month(lastsaledate)

keep if mergetype_c==1
sort idl numdatel
by idl: gen index=_n
replace numdatet=datediff_c+numdatel
*month number of current list price
gen my=(year(numdatel)-1988)*12+month(numdatel)
gen myt=(year(numdatet)-1988)*12+month(numdatet)
drop if myt==298
gen weeknum=numdatel-dow(numdatel)

**get variables that help adjust the list price 
gen seas=mod(my,12)
gen TOM2=TOM*TOM
gen num_lpchanges=min(lpchng,5)
gen chngprice=(num_lpchanges>1)
gen tenure=(numdatel-lastsaledate)/365
gen short=(TOM<=14)
gen medium=(TOM>14&TOM<45)
gen medium2=(TOM>=45&TOM<90)
gen longer=(TOM>=90& TOM<180)
gen expire=(TOM>=174 & TOM<=183)
gen short_chngprice=short*chngprice
gen medium_chngprice=medium*chngprice
gen longer_chngprice=(TOM>=90)*chngprice
gen soon1=chngprice*(lptime<=31)
gen soon2=chngprice*(lptime<=14)
gen indivs = (strpos(sr_seller, ",")~=0)
gen indivb = (strpos(sr_buyer, ",")~=0)
gen noarms=(sr_arms_length_flag!="1")
gen estate=(strpos(sr_buyer,"ESTATE")~=0)
gen family=(strpos(sr_buyer,"FAMILY")~=0)
gen living=(strpos(sr_buyer,"LIVING")~=0)
gen trust=(substr(sr_buyer,-2,2)=="TR")
gen relocat=(strpos(sr_buyer,"RELOC")~=0|strpos(sr_buyer,"MOBILITY")~=0)
replace trust=1 if substr(sr_buyer,-5,5)=="TRUST"
replace trust=1 if substr(sr_buyer,1,5)=="TRUST"
gen foreclosure2=0
replace foreclosure2=1 if strpos(sr_buyer,"BANK")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"BANC")~=0 & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer," BA ")~=0 & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"BK")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"JP")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"MORT")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"MTG")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"HUD-")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if sr_buyer=="HUD"  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"LOA")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"LNS")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"LN")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"SECU")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"ASSET")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"DEUTSCHE")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"STEARNS")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"FARGO")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"SERIE")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"STANLEY")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"MORGAN")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"CITI")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"BOSTON")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"WAMU")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"MUTUAL")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"LYNCH")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"LEHMAN")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"REO ")~=0     & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer," SECS ")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"FEDERAL")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"FDIC")~=0     & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"FRANKLIN")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"COUNTY")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"CLERK")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"SHERIFF")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"INVEST")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"INVS")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"DEFAULT")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"FORECL")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"HOLDING")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"SAVING")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"LENDERS")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"LENDING")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"SACHS")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"DOWNEY")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"FREMONT")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"SUISSE")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"CWALT")~=0    & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"GSAMP")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"2003-")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"2004-")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"2005-")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"2006-")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"2007-")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"2008-")~=0  & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"2009-")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"2010-")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"2011-")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"2012-")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"2013-")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"TD")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"SERVICE")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"SERVICING")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"SVC")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"FUNDING")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"HOMESALES")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0 
replace foreclosure2=1 if strpos(sr_buyer,"ABS")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"LLC")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"FINANCIAL")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"CIT GROUP")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"CIT GRP")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"LIQUIDATION")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"FANNIE")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"FREDDIE")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"BENEFICIAL")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"CALIFORNIA HOUSING")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"CALIFORNIA HSNG")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"DB STRUCTURED PRODUCTS INC")~=0   & indivb==0 & noarms==1 & family==0 & estate==0 & living==0  & relocat==0
replace foreclosure2=1 if strpos(sr_buyer,"REAL")~=0   & indivb==0 & noarms==1 & family==0  & living==0  & relocat==0
rename foreclosure2 foreclosure
replace foreclosure=0 if mergetype_f==0
gen longTOM=(TOM>180)*TOM
gen longTOM2=(TOM>180)*TOM2
gen raise=(ratio>1)
gen ratio_chngprice=ratio*chngprice
gen above=(ln(lastsalepr)-ln(price)>0)
gen sale_list=salepr/price

replace price=salepr
**outliers
gen interval=my-my0
drop if interval<6 
gen temp=abs(ln(price)-ln(lastsalepr))/interval
drop if sale_list>2 | sale_list<0.25
drop if temp>.05
drop if price<20000
drop if lastsalepr<20000
drop if price>4000000
drop if lastsalepr>4000000
drop if my0<thresh
drop temp
gen temp=abs(ln(price)-ln(lastsalepr))/interval


sort numdatel


pctile pct=temp ,nq(100)
gen rw=1
replace rw=.25 if temp>pct[95]
replace rw=.75 if temp>pct[90] & temp<=pct[95]
*this is taken from CS methodology on interval weights 
gen rw2=-.02105*interval/12+1.0105
drop temp

gen dayl=day(numdatel)
gen monthl=month(numdatel)
gen yearl=year(numdatel)

*more variables that are helpful for adjusting list price
gen temp=string(price)
gen digit=substr(temp,-4,1)
destring digit,replace
gen precise=(digit==1|digit==2|digit==3|digit==6|digit==7|digit==8)
gen justbelow=(digit==4|digit==9)
gen digit2=substr(temp,-3,1)
gen digit3=substr(temp,-5,1)
gen digit4=substr(temp,-6,1)
gen hundreds=(digit2!="0")
gen tenthou=(digit3!="0")
destring digit3 digit,replace
gen t1=(digit3==5&digit==0)
gen t2=(digit3==9&digit==9)
gen t3=(digit3==9&digit==5)
gen popular=min(t1+t2+t3,1)

gen listdate=numdatel-TOM

gen wedge2=ln(salepr)-ln(price)
gen wedge3=salepr/price

sort lowaddress numdatel
gen rw3=1
gen adjprice=price

gen id=_n

replace price=price/10000
replace lastsalepr=lastsalepr/10000
replace adjprice=adjprice/10000

egen houseid=group(lowaddress)
sort houseid weeknum

gen weights_1=0

*merge on cs index associated with month of previous sale; this comes in LOGS
sort my0
merge my0 using delta0.dta,nokeep

gen weights_`b'=0
keep if mergetype_c==1 
keep  dayl my my0 lastsalepr price adjprice rw rw2 rw3 id seas houseid weeknum delta0 weights*
order  dayl my my0 lastsalepr price adjprice rw rw2 rw3 id seas houseid weeknum delta0 weights*
outsheet using `j'_salepr_contract.txt,nonames replace
}


